package cn.demengsite.service.impl;

import cn.demengsite.dao.ProductDAO;
import cn.demengsite.dao.ReportDAO;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.PO.OrderProduct;
import cn.demengsite.model.Product;
import cn.demengsite.model.Report;
import cn.demengsite.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.demengsite.base.ProductConfig.productNameMap;

@Repository
public class AdminServiceImpl implements AdminService {

    @Autowired
    private ReportDAO reportDAO;

    @Autowired
    private ProductDAO productDAO;

    @Override
    public BaseInfo getAdminSaleWithBeginEnd(String beginDate, String endDate) {

        Map<String ,Object > backMap = new TreeMap<>();

        //所有货的销售信息
        List<OrderProduct> opList = reportDAO.getAllSaleRePortWithBeginEnd(beginDate, endDate);

        //所有的德蒙的货的信息
        List<OrderProduct> dmList = opList.stream().filter(op-> op.getSaleType()!=3 && !op.getBarCode().startsWith("9")).collect(Collectors.toList());

        //所有散货的销售信息
        List<OrderProduct> otherList = opList.stream().filter(op->op.getSaleType()==3).collect(Collectors.toList());

        /**
         *开始统计德蒙的信息
         */

        // 销售的件数
        int dmcount = dmList.stream().mapToInt(OrderProduct::getProductNum).sum();
        backMap.put("德蒙销售件数：",dmcount);

        // 总成本数
        Double dmtotalCost = dmList.stream().mapToDouble(op-> op.getOriginalPrice()*op.getProductNum()).sum()*0.16;
        backMap.put("德蒙总成本：",dmtotalCost);

        //总销售额
        Double dmtotalSale = dmList.stream().mapToDouble(OrderProduct::getSalePrice).sum();
        backMap.put("德蒙总销售：",dmtotalSale);


        //总盈利
        double dmtotalEarn = (dmtotalSale - dmtotalCost);
        BigDecimal b   =   new   BigDecimal(dmtotalEarn);
        dmtotalEarn   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
        backMap.put("德蒙总盈利：",dmtotalEarn);

        /**
         *开始统计散货的信息
         */
        //销售的件数
        int othercount = otherList.stream().mapToInt(OrderProduct::getProductNum).sum();
        backMap.put("散货总销售件数：",othercount);

        //总销售额
        Double otherTotalSale = otherList.stream().mapToDouble(OrderProduct::getSalePrice).sum();
        backMap.put("散货总销售金额：",otherTotalSale);


        /**
         * 开始统计德蒙的库存信息
         */
        //总库存
        List<Product> products = productDAO.getAllProduct();

        //去除库存为0的数据和预留的会员卡数据
        List<Product> allProducts = products.stream().filter(p-> p.getProductNum()>0&&!p.getBarCode().startsWith("9")).collect(Collectors.toList());




        //计算各个款的总数
        Map<String ,String> styleMap = productNameMap;

        Map<String ,List<Product>> styleProductMap = new HashMap<>();

        for (String s : styleMap.keySet()) {
            List<Product> list = new ArrayList<>();
            styleProductMap.put(styleMap.get(s),list);
        }
        //秋一和秋二
        int auton1 =0;
        int auton2 =0;

        // 19年春款和夏款的数量
        int spring19 = 0;
        int summer19 = 0;

        for (Product product : allProducts) {
            if(product.getBarCode().substring(2,4).equals("31")){
                auton1++;
            }else if (product.getBarCode().substring(2,4).equals("32")){
                auton2++;
            }else if(product.getBarCode().substring(1,3).equals("91")){
                spring19++;
            }else if(product.getBarCode().substring(1,3).equals("92")){
                summer19++;
            }


            for (String s : styleMap.keySet()) {
                if (product.getBarCode().substring(4,6).equals(s)) {
                    styleProductMap.get(styleMap.get(s)).add(product);
                }
            }
        }


        Map<String , Integer> styleNum = new HashMap<>();

        for (String s : styleProductMap.keySet()) {
            styleNum.put(s,styleProductMap.get(s).size());
        }

        backMap.put("秋一的款还剩：",auton1);
        backMap.put("秋二的款还剩：",auton2);
        backMap.put("19年春款还剩：",spring19);
        backMap.put("19年夏款还剩：",summer19);
        backMap.put("其他的库存总计：", styleNum);







        return new BaseInfo("0","OK",backMap);
    }

}
